<?
/**
 * class:         sendmail.class.php
 * description:    class for sending HTML mails with attachments
 * created:        21.02.2003
 * last change:    19.12.2003
 * author:        Guther Bauer <guenni1981@lycos.de>
 * copyright:    Guther Bauer
 */
    class sendmail
    {
        // Declare variables
        private $_emailHeader = "";
        private $_textHeader = "";
        private $_textBoundary = "";
        private $_emailBoundary = "";
        private $_charset = "";
        private $_subject = "";
        private $_receiver = "";
        private $_attachment = array();
        private $_ccMails = array();
        private $_bccMails = array();
        private $_errors = array();

        // Constructor
        function sendmail()
        {
            $this->_textBoundary = uniqid(time());
            $this->_emailBoundary = uniqid(time());
            $this->_charset = "ISO-8859-1";
        }

        
        // Set the CharSet
        function setCharset($char)
        {
            $this->_charset = $char;
        }


        // Check if the e-mail address is correct
        private function _validateEmail($mailAddress)
        {
            return $mailAddress;
        }

        
        // Set Name and From Email at header of mail
        function from($name,$email)
        {
            $this->_emailHeader .= 'From: '.$name.'<'.$email.'>'."\n";
        }

        
        // Set To email
        function to($to)
        {
            $this->_receiver = $this->_validateEmail($to);
        }

        
        // Send a copy to Cc recipients
        function cc($copyToReceiver)
        {
            $this->_ccMails[] = $copyToReceiver;
        }

        
        // Send a copy to Bcc recipients
        function bcc($copyToReceiver)
        {
            $this->_bccMails[] = $copyToReceiver;
        }

        
        // Create the header of the mime-mail
        private function _makeMimeMail()
        {
            if(count($this->_ccMails) > 0)
            {
                $this->_emailHeader .= 'Cc: ';
                for($i=0;$i<count($this->_ccMails);$i++)
                {
                    if($i > 0) $this->_emailHeader .= ',';
                    $this->_emailHeader .= $this->_validateEmail($this->_ccMails[$i]);
                }
                $this->_emailHeader .= "\n";
            }

            if(count($this->_bccMails) > 0)
            {
                $this->_emailHeader .= 'Bcc: ';
                for($j=0;$j<count($this->_bccMails);$j++)
                {
                    if($j > 0) $this->_emailHeader .= ',';
                    $this->_emailHeader .= $this->_validateEmail($this->_bccMails[$j]);
                }
                $this->_emailHeader .= "\n";
            }
            $this->_emailHeader .= 'MIME-Version: 1.0'."\n";
        }

        
        // Set Subject of the mail
        function subject($subject)
        {
            $this->_subject = $subject;
        }

        
        // Pack the text data into mail
        function text($text)
        {
            $this->_textHeader .= 'Content-Type: multipart/alternative; boundary="'.$this->_textBoundary.'"'."\n\n";
            $this->_textHeader .= '--'.$this->_textBoundary."\n";
            $this->_textHeader .= 'Content-Type: text/plain; charset="'.$this->_charset.'"'."\n";
            $this->_textHeader .= 'Content-Transfer-Encoding: quoted-printable'."\n\n";
            $this->_textHeader .= strip_tags($text)."\n\n";
            $this->_textHeader .= '--'.$this->_textBoundary."\n";
            $this->_textHeader .= 'Content-Type: text/html; charset="'.$this->_charset.'"'."\n";
            $this->_textHeader .= 'Content-Transfer-Encoding: quoted-printable'."\n\n";
            $this->_textHeader .= '<html><body>'.$text.'</body></html>'."\n\n";
            $this->_textHeader .= '--'.$this->_textBoundary.'--'."\n\n";
        }

        
        // Attach file into mail
        function attachment($attachedfile,$filename = "")
        {
            // Check if the file exists
            if(is_file($attachedfile))
            {
                // Create attached file name
                if(strlen($filename) == 0){
                    $filename = basename($attachedfile);
                }
                else{
                    $path_parts = pathinfo($attachedfile);
                    $filename = basename($filename,$path_parts['extension']).'.'.$path_parts['extension'];
                    $filename = str_replace("..",".",$filename);
                }
                
                // Generate attachment header
                $attachment_header = '--'.$this->_emailBoundary."\n" ;
                $attachment_header .= 'Content-Type: application/octet-stream; name="'.$filename.'"'."\n";
                $attachment_header .= 'Content-Transfer-Encoding: base64'."\n";
                $attachment_header .= 'Content-Disposition: attachment; filename="'.$filename.'"'."\n\n";

                // Format file in Base64 and split 72 characters per line
                $file['inhalt'] = fread(fopen($attachedfile,"rb"),filesize($attachedfile));
                $file['inhalt'] = base64_encode($file['inhalt']);
                $file['inhalt'] = chunk_split($file['inhalt'],72);

                // Store the attachment with header in variable
                $this->_attachment[] = $attachment_header.$file['inhalt']."\n";
            }
        }

        
        // Create the complete headers of the email
        // Send email
        function send()
        {
            $acceptedList = array();
            $acceptedList[] = "phucngohoang@gmail.com";
            $acceptedList[] = "doanvuthuan@gmail.com";
            $acceptedList[] = "phucnh@cybersoft-vn.com";
            $acceptedList[] = "thuanvd@cybersoft-vn.com";
            $acceptedList[] = "minhdtd@cybersoft-vn.com";
            $acceptedList[] = "rich@planvsoftware.com";
            $acceptedList[] = "orders@digitalpickle.com";
            $acceptedList[] = "orders1@digitalpickle.com";
            $acceptedList[] = "digitalpickle-LA@digitalpickle.com";
            $acceptedList[] = "digitalpickle-SF@digitalpickle.com";
            
            $accepted = in_array(trim(strtolower($this->_receiver)),$acceptedList);
            $pathArr = split("/",$_SERVER['PHP_SELF']);
            $testEnv = ($pathArr[1] == "customerTesting");
            if( (TRUE == $testEnv) && (FALSE == $accepted) )
            {
                return "";
            }
            
            
            $this->_makeMimeMail();

            $header = $this->_emailHeader;

            // Check whether attachments were added
            if(count($this->_attachment)>0)
            {
                $header .= 'Content-Type: multipart/mixed; boundary="'.$this->_emailBoundary.'"'."\n\n";
                $header .= '--'.$this->_emailBoundary."\n";
                $header .= $this->_textHeader;

                if(count($this->_attachment) > 0) $header .= implode("",$this->_attachment);

                $header .= '--'.$this->_emailBoundary.'--';
            }
            else
            {
                $header .= $this->_textHeader;
            }
            // Send Mail
            mail("$this->_receiver",$this->_subject,"",$header);

            $this->_deleteMemory();
        }

        
        // This function is only to test
        private function _deleteMemory()
        {
            unset($this->_emailHeader);
            unset($this->_textHeader);
            unset($this->_attachment);
            unset($this->_errors);
        }
    }
?>